<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

</head>

<body>
    <div id="app">
        <button>doubleCount:{{ doubleCount }}</button>
        <button v-on:click="countPlus">count:{{ count }}</button>
        <button v-on:click="changeDoubleCount">changeDoubleCount</button>
        <div>==========</div>
        <div>方法：{{doubleCount2()}}</div>
        <div>方法：{{doubleCount2()}}</div>
        <div>方法：{{doubleCount2()}}</div>
        <div>计算属性：{{doubleCount}}</div>
        <div>计算属性：{{doubleCount}}</div>
        <div>计算属性：{{doubleCount}}</div>
        <div>===========</div>
        <button v-on:click="age++">{{age}}</button>
        <button v-on:click="agePlusOne++">agePlusOne:{{agePlusOne}}</button>
    </div>


    <!-- 引入vue -->
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script>
        const { createApp, ref, reactive, toRefs, watch, computed } = Vue;
        // 创建Vue实例
        const app = createApp({
            setup() {
                const count = ref(1)
                const age = ref(18)
                // 计算属性 返回的是一个ref对象  是只读对象，不能修改   ,有缓存
                const doubleCount = computed(() => {
                    console.log("计算属性被调用");

                    return count.value * 2
                })




                const doubleCount2 = () => {
                    console.log("方法被调用");

                    return count.value * 2
                }
                console.log("doubleCount:", doubleCount);

                const countPlus = () => {
                    count.value++
                }
                const changeDoubleCount = () => {
                    doubleCount.value++
                }

                // ======================
                const agePlusOne = computed({
                    // getter setter
                    get() { // 必须要有返回值
                        console.log("agePlusOne被读取");

                        return age.value
                    },
                    set(newVal) {
                        console.log("agePlusOne被修改");

                        age.value = newVal
                    }
                })

                return {
                    count,
                    doubleCount,
                    age,
                    agePlusOne,
                    countPlus,
                    changeDoubleCount,
                    doubleCount2
                }
            }
        })
        // 挂载到dom
        app.mount('#app')
    </script>
</body>

</html>